package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.RecordActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.FileBean;
import flc.ast.databinding.FragmentVideoBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.UriUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VideoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VideoFragment extends BaseNoModelFragment<FragmentVideoBinding> {

    private RecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llVideoTailor.setOnClickListener(this);
        mDataBinding.llVideoSplice.setOnClickListener(this);
        mDataBinding.llVideoBackground.setOnClickListener(this);
        mDataBinding.llVideoFilter.setOnClickListener(this);
        mDataBinding.llVideoRotate.setOnClickListener(this);
        mDataBinding.llVideoCompress.setOnClickListener(this);
        mDataBinding.tvEditRecord.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llVideoTailor://视频裁剪
                SelVideoActivity.type = 2;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.llVideoSplice://视频拼接
                SelVideoActivity.type = 3;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.llVideoBackground://视频背景
                SelVideoActivity.type = 4;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.llVideoFilter://视频滤镜
                SelVideoActivity.type = 5;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.llVideoRotate://视频旋转
                SelVideoActivity.type = 6;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.llVideoCompress://视频压缩
                SelVideoActivity.type = 7;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.tvEditRecord://编辑记录
                startActivity(RecordActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(UriUtil.getVideoUri(mContext, recordAdapter.getItem(position).getPath()), "video/*");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecord;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                list.add(new FileBean(file.getPath()));
            }
        }
        if (list.size() > 0) {
            recordAdapter.setList(list);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.ivRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.ivRecordNoData.setVisibility(View.VISIBLE);
        }
    }

}